# 初始化系统数据
products = [
    {"name": "空调", "price": 3000},
    {"name": "电视", "price": 2000},
    {"name": "冰箱", "price": 2500},
    {"name": "洗衣机", "price": 1800},
    {"name": "微波炉", "price": 500}
]

cart = []  # 购物车
money = 5000  # 初始金额


def show_products():
    """显示商品列表"""
    print("\n=== 商品清单 ===")
    for idx, item in enumerate(products, 1):
        print(f"{idx}. {item['name']} ￥{item['price']}")


def print_receipt():
    """打印购物小票"""
    print("\n=== 购物小票 ===")
    total = 0
    for item in cart:
        print(f"{item['name']:　<6} ￥{item['price']:>6}")
        total += item["price"]
    print("-" * 20)
    print(f"消费总额：￥{total}")
    print(f"剩余余额：￥{money}")


def shopping_system():
    global money
    print("欢迎来到智能购物系统！")
    print(f"您当前的余额为：￥{money}")
    show_products()

    while True:
        choice = input("\n请输入商品编号/名称（Q/q结账）：").strip()

        # 退出机制
        if choice.lower() == "q":
            print_receipt()
            print("\n感谢光临，欢迎下次再来！")
            return

        # 处理数字输入
        if choice.isdigit():
            index = int(choice) - 1
            if 0 <= index < len(products):
                selected = products[index]
            else:
                print("商品编号不存在！")
                continue
        else:  # 处理名称输入
            selected = None
            for item in products:
                if item["name"] == choice:
                    selected = item
                    break
            if not selected:
                print("商品不存在！")
                continue

        # 余额验证
        if money >= selected["price"]:
            cart.append(selected)
            money -= selected["price"]
            print(f"成功购买【{selected['name']}】，当前余额：￥{money}")
        else:
            print(f"余额不足！商品价格：￥{selected['price']}，当前余额：￥{money}")


if __name__ == "__main__":
    shopping_system()
